package com.zhou.system.management.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zhou.common.lib.model.Response;
import com.zhou.system.management.entity.SysDept;
import com.zhou.system.management.model.ao.SysDeptAddAo;
import com.zhou.system.management.model.ao.SysDeptEnableAo;
import com.zhou.system.management.model.ao.SysDeptUpdateAo;
import com.zhou.system.management.service.impl.SysDeptServiceImpl;
import com.zhou.system.management.validgroup.BaseGroup;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zxf
 * @since 2023-05-18
 */
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {

    @Autowired
    private SysDeptServiceImpl deptService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询部门列表")
    @PostMapping("/query")
    public Response<List<SysDept>> query() {
        LambdaQueryWrapper<SysDept> queryWrapper = Wrappers.<SysDept>lambdaQuery()
                .orderByAsc(SysDept::getOrderNum)
                .orderByAsc(SysDept::getDeptId);
        List<SysDept> list = deptService.list(queryWrapper);
        return Response.success(list);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增部门")
    @PostMapping("/add")
    public Response add(@Validated @RequestBody SysDeptAddAo addVo) {
        deptService.add(addVo);
        return Response.success();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "更新部门")
    @PostMapping("/update")
    public Response<String> update(@Validated(BaseGroup.UpdateGroup.class) @RequestBody SysDeptUpdateAo updateVo) {
        deptService.update(updateVo);
        return Response.success();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询单个用户组织")
    @PostMapping("/{deptId}")
    public Response<SysDept> queryOne(@PathVariable Long deptId) {
        SysDept sysDept = deptService.getById(deptId);
        return Response.success(sysDept);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{deptId}")
    public Response<String> update(@PathVariable Long deptId) {
        //todo 部门下面有子部门，或部门下面有用户，则不能删除
        deptService.removeById(deptId);
        return Response.success();
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询用户组织信息")
    @PostMapping("/enableDept")
    public Response enableDept(@Validated @RequestBody SysDeptEnableAo ao) {
        deptService.enableDept(ao);
        return Response.success();
    }


}
